<?php
/**
 * Created by IntelliJ IDEA.
 * User: dhy
 * Date: 2015/9/11
 * Time: 15:54
 */

namespace App\Http\Middleware\Admin\Auth;
use App\Http\Models\Admin\Auth\Resource;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use App\Http\Common\Tree;
class AdAuthMiddleware {
    public function handle($request,  $next){

//        var_dump(     );
        $adUser = $request->session()->get('adminUser');
        if ($adUser == null) {
            return redirect('admin/login');
        }

        if($adUser->auths == null){
            //如果没有权限信息,补充权限信息
            $auths = array();
            //添加用户的权限信息
            if($adUser->is_admin == 1){
                $auths = DB::select("SELECT * from resource   order by order_index asc");

            }else{
                $auths = DB::select("SELECT * from resource where id in ( SELECT res_id from role_resource where role_id in (select roleid from user_role where userid = :userid )) order by order_index asc",["userid"=>$adUser->id]);
            }
            $adUser->auths = $this->authBuild($auths);
        }
//        $adUser->current = Config::get("common.".str_replace(".","_",Route::currentRouteName()));

//        var_dump(Config::get("common.".str_replace(".","_",Route::currentRouteName())));
        $ASSETS = array();
        //添加当前路由下需要添加的js和css, 配置项在config/asstes.php. 注意路由转换, 如admin.userinfo 那么在config里面为admin_userinfo
        $addJS = Config::get('assets.'.str_replace(".","_",Route::currentRouteName())."_js");
        $addCSS = Config::get('assets.'.str_replace(".","_",Route::currentRouteName())."_css");
        if($addJS){
            $ASSETS['JS'] =  $addJS;
        };
        if($addCSS){
            $ASSETS['CSS'] =  $addCSS;
        };



        //acitve当前url
        $url = request()->route()->getUri();
        if($url != ""){

            $find = Resource::where('res_url',"/".$url)->first();
            $ASSETS['currentCategory'] = $find;
//            if($find){
//
//                $ASSETS['currentSubCategory'] = $find->id;
//                $ASSETS['currentCategoryName'] = $find->name;
//                $ASSETS['currentCategory'] = $find->parent_id;
//            }
        }
        $request['ASSETS'] = $ASSETS;

        return $next($request);
    }


    public function  authBuild($auths){
        $results =  array();

        foreach ($auths as $parent){
            if($parent->parent_id == null || $parent->parent_id == 0 || $parent->parent_id == ""){
                $results[] =$parent;
                //构造二级
                $children = $this->getChildrens($auths,$parent->id);
                if(count($children) >0){
                    $parent->children = $children;
                }

            }
        }
//        var_dump($results);
        return $results;

    }

    public  function  getChildrens($resouces,$pid){
        $childrens = array();
        foreach ($resouces as $item){
            if($item->parent_id == $pid){
                $childrens[] = $item;
            }
        }
        return $childrens;
    }

}